﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmQuanLyHangHoa : Form
    {
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private KhoManagement khoMgt = new KhoManagement();
        private CTKhoManagement ctkhoMgt = new CTKhoManagement();
        private KhayDungManagement khaydungMgt = new KhayDungManagement();
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private DonViTinhMgt donvitinhMgt = new DonViTinhMgt();

        bool IsEdited = false;

        public FrmQuanLyHangHoa()
        {
            InitializeComponent();
        }

        #region Xử lý Sự kiện

        private void FrmQuanLyHangHoa_Load(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            groupBox1.Enabled = false;
            groupBox2.Enabled = false;
            ClearTextBox();
            BindingToGrid();

            cboDonViTinh.DataSource = GetAllDonViTinh();
            cboDonViTinh.DisplayMember = "TenDVT";
            cboDonViTinh.ValueMember = "Id";
            cboDonViTinh.Text = "";

            cboKhayDung.DataSource = GetAllKhayDung();
            cboKhayDung.DisplayMember = "Ten";
            cboKhayDung.ValueMember = "Id";
            cboKhayDung.Text = "";

            cboKho.DataSource = GetAllKho();
            cboKho.DisplayMember = "TenKho";
            cboKho.ValueMember = "Id";

            cboNoiLuuTru.DataSource = GetAllChiNhanhCongTy();
            cboNoiLuuTru.DisplayMember = "TenChiNhanh";
            cboNoiLuuTru.ValueMember = "Id";
        }

        public void ClearTextBox()
        {
            txtMaSo.Clear();
            txtTenHang.Clear();
            txtDonGia.Clear();
            txtSoLuongBD.Clear();
            txtGiaTriBD.Clear();
        }

        public void BindingToGrid()
        {
            dgvQuanLyHangHoa.AutoGenerateColumns = false;
            dgvQuanLyHangHoa.DataSource = GetAllHangHoa();
        }

        private void dgvQuanLyHangHoa_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                btnSua.Enabled = true;
                btnLuu.Enabled = false;
                btnSua.Enabled = true;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;

                lblIdHH.Text = dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[0].Value.ToString();
                txtMaSo.Text = dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[1].Value.ToString();
                txtTenHang.Text = dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[2].Value.ToString();
                cboDonViTinh.Text = dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[3].Value.ToString();
                cboNoiLuuTru.Text = dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[6].Value.ToString();
                txtDonGia.Text = dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[5].Value.ToString();

                cboKhayDung.Text = "";
                if (Convert.ToInt32(dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[7].Value) != -1)
                {
                    KhayDung khaydung = (from p in GetAllKhayDung()
                                         where p.Id == Convert.ToInt32(dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[7].Value)
                                         select p).FirstOrDefault();

                    cboKhayDung.Text = khaydung.Ten;
                }

                int idHangHoa = Convert.ToInt32(dgvQuanLyHangHoa.Rows[e.RowIndex].Cells[0].Value);
                CTKho ctKho = (from ctk in GetAllCTKho()
                               where ctk.IdHangHoa == idHangHoa
                               select ctk).FirstOrDefault();

                lblIdCTKho.Text = ctKho.Id.ToString();
                txtSoLuongBD.Text = ctKho.SoLuongBanDau.ToString();
                txtGiaTriBD.Text = ctKho.GiaTriBanDau.ToString();

                Kho kho = (from k in GetAllKho()
                           where k.Id == ctKho.IdKho
                           select k).FirstOrDefault();

                cboKho.Text = kho.TenKho;
            }
        }

        private void cboKhayDung_Leave(object sender, EventArgs e)
        {
            foreach (KhayDung b in cboKhayDung.Items)
            {
                if (b.Ten.ToLower().Contains(cboKhayDung.Text) && cboKhayDung.Text != "")
                {
                    cboKhayDung.SelectedValue = b.Id;
                    break;
                }
            }

            if (cboKhayDung.SelectedValue == null)
            {
                cboKhayDung.Text = "";
            }
        }

        private void cboNoiLuuTru_Leave(object sender, EventArgs e)
        {
            foreach (ChiNhanhCongTy c in cboNoiLuuTru.Items)
            {
                if (c.TenChiNhanh.ToLower().Contains(cboNoiLuuTru.Text) && cboNoiLuuTru.Text != "")
                {
                    cboNoiLuuTru.SelectedValue = c.Id;
                    break;
                }
            }

            if (cboNoiLuuTru.SelectedValue == null)
            {
                cboNoiLuuTru.Text = "";
                cboNoiLuuTru.Focus();
            }
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        private void txtSoLuongBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        private void txtGiaTriBD_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        #endregion

        #region Xử lý Button

        private void btnThem_Click(object sender, EventArgs e)
        {
            btnLuu.Enabled = true;
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;

            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            ClearTextBox();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtMaSo.Text == "" || txtTenHang.Text == "" || txtDonGia.Text == "" || txtGiaTriBD.Text == "" || txtSoLuongBD.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin !", "Thông báo");
            }
            else
            {
                ResultObj result = new ResultObj();

                HangHoa hanghoa = new HangHoa();
                hanghoa.MaSo = txtMaSo.Text;
                hanghoa.Ten = txtTenHang.Text;
                hanghoa.DonViTinh = cboDonViTinh.Text;
                hanghoa.DonGia = Convert.ToInt64(txtDonGia.Text);
                hanghoa.ChuoiTimKiem = "";
                hanghoa.SoLuong = Convert.ToInt64(txtSoLuongBD.Text);
                hanghoa.NoiLuuTru = ((ChiNhanhCongTy)cboNoiLuuTru.SelectedItem).TenChiNhanh;
                hanghoa.IdKhayDung = cboKhayDung.SelectedItem == null ? -1 : ((KhayDung)cboKhayDung.SelectedItem).Id;

                if (!IsEdited)
                {
                    result = hanghoaMgt.SaveNewHangHoa(hanghoa);
                    lblIdHH.Text = result.ReturnCode.ToString();
                }
                else
                {
                    hanghoa.Id = Convert.ToInt32(lblIdHH.Text);
                    result = hanghoaMgt.UpdateHangHoa(hanghoa);
                }

                if (result.ReturnCode > 0)
                {
                    CTKho ctkho = new CTKho();
                    ctkho.SoLuongBanDau = Convert.ToInt64(txtSoLuongBD.Text);
                    ctkho.GiaTriBanDau = Convert.ToInt64(txtGiaTriBD.Text);
                    ctkho.IdHangHoa = Convert.ToInt32(lblIdHH.Text);
                    ctkho.IdKho = ((Kho)cboKho.SelectedItem).Id;

                    if (!IsEdited)
                    {
                        result = ctkhoMgt.SaveNewCTKho(ctkho);
                    }
                    else
                    {
                        ctkho.Id = Convert.ToInt32(lblIdCTKho.Text);
                        result = ctkhoMgt.UpdateCTKho(ctkho);
                        IsEdited = false;
                    }

                    if (result.ReturnCode > 0)
                        MessageBox.Show("Lưu thành công", "Thông báo");
                    else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                        MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    else
                        MessageBox.Show("Thất bại");
                }

                btnLuu.Enabled = false;
                btnSua.Enabled = true;
                groupBox1.Enabled = false;
                groupBox2.Enabled = false;
            }

            BindingToGrid();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            groupBox1.Enabled = true;
            groupBox2.Enabled = true;

            IsEdited = true;
            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Bạn có chắc chắn muốn xóa hàng hóa này không?", "Xóa hàng hóa", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                ResultObj resultObj = new ResultObj();

                HangHoa hanghoa = new HangHoa();
                hanghoa.MaSo = txtMaSo.Text;
                hanghoa.Ten = txtTenHang.Text;
                hanghoa.DonViTinh = cboDonViTinh.Text;
                hanghoa.DonGia = Convert.ToInt64(txtDonGia.Text);
                hanghoa.ChuoiTimKiem = "";
                hanghoa.SoLuong = Convert.ToInt64(txtSoLuongBD.Text);
                hanghoa.NoiLuuTru = ((ChiNhanhCongTy)cboNoiLuuTru.SelectedItem).TenChiNhanh;
                hanghoa.IdKhayDung = cboKhayDung.SelectedItem == null ? -1 : ((KhayDung)cboKhayDung.SelectedItem).Id;
                hanghoa.Id = Convert.ToInt32(lblIdHH.Text);
                hanghoa.IsDelete = 1;

                resultObj = hanghoaMgt.UpdateHangHoa(hanghoa);

                btnLuu.Enabled = true;
                btnSua.Enabled = false;
                btnXoa.Enabled = false;

                ClearTextBox();
                BindingToGrid();
            }
        }

        #endregion

        #region Get dữ liệu

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj result = new ResultObj();
            result = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> lstChiNhanhCongTy = (List<ChiNhanhCongTy>)result.Result;
            return lstChiNhanhCongTy;
        }

        public List<KhayDung> GetAllKhayDung()
        {
            ResultObj result = new ResultObj();
            result = khaydungMgt.GetAllKhayDung();
            List<KhayDung> lstKhayDung = (List<KhayDung>)result.Result;
            return lstKhayDung;
        }

        public List<DonViTinh> GetAllDonViTinh()
        {
            ResultObj result = new ResultObj();
            result = donvitinhMgt.GetAllDonViTinh();
            List<DonViTinh> lstDonViTinh = (List<DonViTinh>)result.Result;
            return lstDonViTinh;
        }

        public List<Kho> GetAllKho()
        {
            ResultObj result = new ResultObj();
            result = khoMgt.GetAllKho();
            List<Kho> lstKho = (List<Kho>)result.Result;
            return lstKho;
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj result = new ResultObj();
            result = hanghoaMgt.GetAllHangHoa();
            List<HangHoa> lstHangHoa = (List<HangHoa>)result.Result;
            return lstHangHoa;
        }

        public List<CTKho> GetAllCTKho()
        {
            ResultObj result = new ResultObj();
            result = ctkhoMgt.GetAllCTKho();
            List<CTKho> lstCTKho = (List<CTKho>)result.Result;
            return lstCTKho;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> listTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;

            return listTaiKhoanKeToan;
        }

        #endregion
    }
}
